#include "DoubleSlider.h"


DoubleSlider::DoubleSlider(QWidget *parent)
	: QWidget(parent)
{
	this->setMinimumHeight(20);
	selected = false;
	connectedSectionBox = false;
}

DoubleSlider::~DoubleSlider()
{

}

void DoubleSlider::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	draw(&painter);

}

void DoubleSlider::draw(QPainter* painter)
{
	//painter->setBrush(QBrush(Qt::white, Qt::SolidPattern));
	painter->setPen(QPen(Qt::NoPen));
	painter->setRenderHint(QPainter::Antialiasing, true);
	painter->setBrush(QBrush(Qt::GlobalColor::cyan, Qt::SolidPattern));
	painter->drawRect(slider1);
	painter->setBrush(QBrush(Qt::GlobalColor::yellow, Qt::SolidPattern));


	for (int i = 0; i < 2; i++)
	{
		if (sliderSelected[i])
		{
			painter->setBrush(Qt::cyan);
			painter->setPen(QPen(Qt::cyan, 2, Qt::SolidLine, Qt::RoundCap));
			painter->drawEllipse(m_slider[i]);
		}
		else if (slider2Selected)
		{
			painter->setBrush(Qt::white);
			painter->setPen(QPen(Qt::cyan, 2, Qt::SolidLine, Qt::RoundCap));
			painter->drawEllipse(m_slider[i / 2 * 2]);
			painter->drawEllipse(m_slider[i / 2 * 2 + 1]);
		}
		else
		{
			painter->setPen(QPen(Qt::lightGray, 2, Qt::SolidLine, Qt::RoundCap));
			painter->setBrush(Qt::cyan);
			painter->drawEllipse(m_slider[i]);
		}
	}
}



void DoubleSlider::mousePressEvent(QMouseEvent *event)
{
	QPointF point = mapFromGlobal(QCursor::pos());
	prePoint = point;
	for (int i = 0; i < 2; i++)
	{
		if (isPointInRect(point, m_slider[i]))
		{
			sliderSelected[i] = true;
			selected = true;
		}
		else
		{
			sliderSelected[i] = false;
		}
	}
	if (!selected)
	{

		if (isPointBetweenTwoRects(point, m_slider[0], m_slider[1]))
		{
			slider2Selected = true;
			selected = true;
		}
		else
		{
			slider2Selected = false;
		}

	}

	update();
}

void DoubleSlider::mouseMoveEvent(QMouseEvent *event)
{
	QPointF point = event->pos();

	for (int i = 0; i < 2; i++)
	{
		if (sliderSelected[i])
		{
			QPointF pointmoveto(point.x() - prePoint.x(), 0);
			if (i % 2 == 0)//left slider
			{
				pointmoveto = m_slider[i].right() + pointmoveto.x() <= m_slider[i + 1].left() ? pointmoveto : QPointF(m_slider[i + 1].left() - m_slider[i].right(), 0);
				pointmoveto = m_slider[i].left() + pointmoveto.x() >= 0 ? pointmoveto : QPointF(0 - m_slider[i].left(), 0);
				m_slider[i].translate(pointmoveto);
			}
			else
			{
				pointmoveto = m_slider[i].right() + pointmoveto.x() < width() ? pointmoveto : QPointF(width() - m_slider[i].right(), 0);
				pointmoveto = m_slider[i].left() + pointmoveto.x() >= m_slider[i - 1].right() ? pointmoveto : QPointF(m_slider[i].left() - m_slider[i - 1].right(), 0);
				m_slider[i].translate(pointmoveto);
			}
		}
	}

	if (slider2Selected)
	{
		QPointF pointmoveto(point.x() - prePoint.x(), 0);
		pointmoveto = (m_slider[0].left() + pointmoveto.x() > 0) ? pointmoveto : (QPointF(0 - m_slider[0].left(), 0));
		pointmoveto = m_slider[1].right() + pointmoveto.x() < width() ? pointmoveto : QPointF(width() - m_slider[1].right(), 0);
		m_slider[0].translate(pointmoveto);
		m_slider[1].translate(pointmoveto);
	}

	prePoint = point;
	updateSectionBox();
	update();
}

void DoubleSlider::mouseReleaseEvent(QMouseEvent *event)
{
	for (int i = 0; i < 2; i++)
	{
		sliderSelected[i] = false;
		slider2Selected = false;
		selected = false;
	}
	update();
}

void DoubleSlider::resizeEvent(QResizeEvent *)
{
	reset();
}

bool DoubleSlider::isPointInRect(QPointF point, QRectF rect)
{
	return ((point.x() >= rect.left()) && (point.x() <= rect.right()) && (point.y() >= rect.top()) && (point.y() <= rect.bottom()));
}

bool DoubleSlider::isPointBetweenTwoRects(QPointF point, QRectF rect1, QRectF rect2)
{
	return (point.x() >= rect1.left() && (point.x() <= rect2.right()) && (point.y() >= rect1.top()) && (point.y() <= rect1.bottom()));
}

void DoubleSlider::updateSectionBox()
{
	m_value[0] = m_slider[0].left() / width();
	m_value[1] = m_slider[1].left() / width();

	for (int i = 0; i < 2; i++) {
		if(sliderSelected[i])
		{ 
			if(i==0)
				emit slider0Change(m_value[i] * m_max);
			if (i == 1)
				emit slider1Change(m_value[i] * m_max);
		}
	}
	if (slider2Selected) {
		emit slider0Change(m_value[0] * m_max);
		emit slider1Change(m_value[1] * m_max);

	}
}

void DoubleSlider::reset()
{
	m_slider[0] = QRectF(m_value[0] * width(), height() - 15, 10, height()*0.5);
	m_slider[1] = QRectF(m_value[1] * width() - 10, height() - 15, 10, height()*0.5);

	slider1 = QRectF(0, 0.15*height()+5, width(), 0.1*height());

}

void DoubleSlider::reset(float min, float max)
{
	m_value[0] = min;
	m_value[1] = max;
	reset();
}


